home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Franz PD / Franz PD Disk #009 (19xx)(Amiga User Group Deutschland e.V.).zip / Franz PD Disk #009 (19xx)(Amiga User Group Deutschland e.V.).adf / DEMOlition / DEMOlition.c < prev    next >
C/C++ Source or Header  |  1986-10-22  |  6KB  |  319 lines

  1. /*    :ts=8
  2.  
  3.     DEMOlition - A display hack inspired by a demo seen on a BBC micro,
  4.              a long time ago.
  5.     This program uses Stephen Coy's "Melt" demo as a template, to make
  6.     a copy of the Workbench screen and handle the close gadget. Only
  7.     the action code is different. Stephen placed no restriction on the
  8.     usage of his code, so it is only fair that I should not place any
  9.     restrictions in mine. Use, abuse, modify or mutilate it as you
  10.     see fit.
  11.             Kriton Kyrimis (princeton!kyrimis)
  12. */
  13.     
  14. #include <exec/types.h>
  15. #include <intuition/intuition.h>
  16.  
  17. #define DEPTH    (SHORT) 2
  18.  
  19. #define NE 0
  20. #define SE 1
  21. #define SW 2
  22. #define NW 3
  23.  
  24. #define rndcol(c)    SetRGB4 (vp, (long)(c), (long)(RangeRand(16)-1), \
  25.                         (long)(RangeRand(16)-1), \
  26.                         (long)(RangeRand(16)-1))
  27.  
  28. extern void    *OpenLibrary(), *OpenWindow(), *OpenScreen(), *GetMsg();
  29. extern long RangeRand(), ReadPixel();
  30.  
  31. struct NewScreen scrdef = {
  32.     (SHORT)0, (SHORT)0, (SHORT)0, (SHORT)0, DEPTH,
  33.     (UBYTE)0, (UBYTE)1,
  34.     (USHORT) 0,
  35.     CUSTOMSCREEN,
  36.     NULL,
  37.     (UBYTE *) "FooBar",
  38.     NULL,
  39.     NULL
  40. };
  41.  
  42. struct NewWindow windef = {
  43.     (SHORT) 0, (SHORT) 30,
  44.     (SHORT) 180, (SHORT) 10,
  45.     (UBYTE) -1, (UBYTE) -1,
  46.     CLOSEWINDOW,
  47.     WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH |  SMART_REFRESH | ACTIVATE,
  48.     NULL,
  49.     NULL,
  50.     (UBYTE *) "DEMOlition",
  51.     NULL,
  52.     NULL,
  53.     (SHORT) 0, (SHORT) 0,
  54.     (SHORT) 0, (SHORT) 0,
  55.     WBENCHSCREEN
  56. };
  57.  
  58. struct Screen    *scr;
  59. struct Window    *win;
  60. struct TmpRas    tmpras;
  61. struct RastPort    *rp;
  62. struct ViewPort *vp;
  63. void        *IntuitionBase, *GfxBase;
  64. int        x, y,
  65.         dx, dy,
  66.         newx, newy,
  67.         maxx, maxy;
  68.  
  69. main()
  70. {
  71.     struct Screen    *wb;
  72.     struct BitMap    *wbm, *mbm;
  73.     long        x, y, n;
  74.     register int    i;
  75.  
  76.     openstuff();
  77.  
  78.     wb = win->WScreen;
  79.     scrdef.LeftEdge  = wb->LeftEdge;
  80.     scrdef.TopEdge   = wb->TopEdge;
  81.     scrdef.Width     = wb->Width;
  82.     scrdef.Height    = wb->Height;
  83.     scrdef.ViewModes = wb->ViewPort.Modes;
  84.     if(!(scr = OpenScreen(&scrdef)))
  85.       die("Screen open failed!");
  86.     ScreenToBack(scr);
  87.  
  88.     rp = &scr->RastPort;
  89.     vp = &scr->ViewPort;
  90.     mbm = rp->BitMap;
  91.     wbm = win->WScreen->RastPort.BitMap;
  92.     BltBitMap(wbm, 0L, 0L, mbm, 0L, 0L,
  93.           (long)scrdef.Width, (long)scrdef.Height,
  94.           0xc0L, 0xffL, NULL);
  95.     ScreenToFront(scr);
  96.     demo();
  97.     closestuff();
  98. }
  99.  
  100. demo()
  101. {
  102.     int    class;
  103.     struct    IntuiMessage    *message;
  104.     int    dir;
  105.     int    c;
  106.  
  107.     maxx = scr->Width - 2;
  108.     maxy = scr->Height - 2;
  109.     x = newx = RangeRand((long)(maxx));
  110.     y = newy = RangeRand((long)(maxy));
  111.     dir = RangeRand(3L);
  112.     dx = 1;
  113.     dy = 1;
  114.     SetAPen(rp, 3L);
  115.     show();
  116.  
  117.     FOREVER {
  118.       message = (struct IntuiMessage *)GetMsg(win->UserPort);
  119.       if(message != (struct IntuiMessage *)NULL) {
  120.         class = message->Class;
  121.         ReplyMsg(message);
  122.         if(class == CLOSEWINDOW) {
  123.           return;
  124.         }
  125.       }
  126.  
  127.       hide();
  128.       switch (dir) {
  129.         case NE:
  130.           newx = x + dx;
  131.           newy = y - dy;
  132.           if (newx > maxx){
  133.         dir = NW;
  134.         rndcol(0);
  135.           }
  136.           else
  137.         if (newy < 0){
  138.           dir = SE;
  139.           rndcol(0);
  140.         }
  141.         else
  142.           if( (c = ReadPixel(rp, (long)newx, (long)newy)) != 0L) {
  143.             explode(c);
  144.             rndcol(c);
  145.             dir = NW;
  146.           }
  147.           break;
  148.         case SE:
  149.           newx = x + dx;
  150.           newy = y + dy;
  151.           if (newx > maxx){
  152.             dir = SW;
  153.         rndcol(0);
  154.           }
  155.           else
  156.             if (newy > maxy){
  157.           dir = NE;
  158.           rndcol(0);
  159.         }
  160.         else
  161.           if( (c = ReadPixel(rp, (long)newx, (long)newy)) != 0L) {
  162.             explode(c);
  163.             rndcol(c);
  164.             dir = NE;
  165.           }
  166.           break;
  167.         case SW:
  168.           newx = x - dx;
  169.           newy = y + dy;
  170.           if (newx < 0){
  171.             dir = SE;
  172.         rndcol(0);
  173.           }
  174.           else
  175.             if (newy > maxy){
  176.           dir = NW;
  177.           rndcol(0);
  178.         }
  179.         else
  180.           if( (c = ReadPixel(rp, (long)newx, (long)newy)) != 0L) {
  181.             explode(c);
  182.             rndcol(c);
  183.             dir = SE;
  184.           }
  185.           break;
  186.         case NW:
  187.           newx = x - dx;
  188.           newy = y - dy;
  189.           if (newx < 0){
  190.             dir = NE;
  191.         rndcol(0);
  192.           }
  193.           else
  194.             if (newy < 0){
  195.           dir = SW;
  196.           rndcol(0);
  197.         }
  198.         else
  199.           if( (c = ReadPixel(rp, (long)newx, (long)newy)) != 0L) {
  200.             explode(c);
  201.             rndcol(c);
  202.             dir = SW;
  203.           }
  204.           break;
  205.       }
  206.       show();
  207.     }
  208. }
  209.  
  210. show()
  211. {
  212.   static int do_delay = 0;
  213.   x = newx;
  214.   y = newy;
  215.  
  216.   SetAPen(rp, 3L);
  217.   SetDrMd(rp, JAM1);
  218.   WritePixel(rp, (long)x    , (long)y);
  219.  
  220.   SetDrMd(rp, COMPLEMENT);
  221.   WritePixel(rp, (long)(x+1), (long)y);
  222.   WritePixel(rp, (long)x    , (long)(y+1));
  223.   WritePixel(rp, (long)(x+1), (long)(y+1));
  224.  
  225.   if (do_delay) {
  226.     Delay(1L);
  227.     do_delay = 0;
  228.   }else{
  229.     do_delay = 1;
  230.   }
  231. }
  232.  
  233. hide()
  234. {
  235.   SetAPen(rp, 0L);
  236.   SetDrMd(rp, JAM1);
  237.   WritePixel(rp, (long)x    , (long)y);
  238.  
  239.   SetDrMd(rp, COMPLEMENT);
  240.   WritePixel(rp, (long)(x+1), (long)y);
  241.   WritePixel(rp, (long)x    , (long)(y+1));
  242.   WritePixel(rp, (long)(x+1), (long)(y+1)); 
  243. }
  244.  
  245. #define DELIM 0xff
  246.  
  247. explode(c)
  248.   int c;
  249. {
  250.   static int ex[] = {-3,  0,  1,  4,        DELIM,
  251.              -2,  0,  1,  3,        DELIM,
  252.              -1,  0,  1,  2,        DELIM,
  253.              -3, -2, -1,  0,  1,    DELIM,
  254.               0,  1,  2,  3,  4,    DELIM,
  255.              -1,  0,  1,  2,        DELIM,
  256.              -2,  0,  1,  3,        DELIM,
  257.              -3,  0,  1,  4,        DELIM};
  258.   int ey;
  259.   int i;
  260.   int xx, yy;
  261.  
  262.   SetAPen(rp, 0L);
  263.   SetDrMd(rp, JAM1);
  264.  
  265.   ey = -3;
  266.   i = 0;
  267.   while (ey < 5) {
  268.     if( ex[i] == DELIM) {
  269.       i++;
  270.       ey++;
  271.       continue;
  272.     }
  273.     xx = newx + ex[i];
  274.     yy = newy + ey;
  275.     if( (xx >= 0) && (yy >= 0) && (xx <= maxx) && (yy <= maxy) )
  276.       WritePixel(rp, (long)(xx), (long)yy);
  277.     i++;
  278.   }
  279.   SetRGB4 (vp, (long)c, (long)(RangeRand(16)-1),
  280.             (long)(RangeRand(16)-1),
  281.             (long)(RangeRand(16)-1));
  282. }
  283.  
  284. openstuff()
  285. {
  286.     if(!(IntuitionBase = OpenLibrary("intuition.library", 0L))) {
  287.       puts("Intuition missing.");
  288.       exit(100);
  289.     }
  290.  
  291.     if(!(GfxBase = OpenLibrary("graphics.library", 0L))) {
  292.       puts("Art shop clode.");
  293.       exit(100);
  294.     }
  295.  
  296.     if(!(win = OpenWindow(&windef))) {
  297.       puts("Window painted shut.");
  298.       closestuff();
  299.       exit(100);
  300.     }
  301. }
  302.  
  303. die(str)
  304. char    *str;
  305. {
  306.     puts(str);
  307.     closestuff();
  308.     exit(100);
  309. }
  310.  
  311. closestuff()
  312. {
  313.     if(tmpras.RasPtr) FreeRaster(tmpras.RasPtr, 320L, 200L);
  314.     if(scr)           CloseScreen(scr);
  315.     if(win)           CloseWindow(win);
  316.     if(GfxBase)       CloseLibrary(GfxBase);
  317.     if(IntuitionBase) CloseLibrary(IntuitionBase);
  318. }
  319.